import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 1237. 找出给定方程的正整数解
 * https://leetcode-cn.com/problems/find-positive-integer-solution-for-a-given-equation/
 */
public class Solutions_1237 {
    public static void main(String[] args) {
        CustomFunction customFunction = new CustomFunction();
//        int z = 5;  // output: {{1, 4}, {2, 3}, {3, 2}, {4, 1}}  调用加法
        int z = 5;  // output: {{1, 5}, {5, 1}}  调用乘法

        List<List<Integer>> result = findSolution(customFunction, z);
        System.out.println(result);
    }

    /**
     * 二分查找，由暴力法优化而来
     * 判断第 i 个数，是否有 [1, 1000] 中是否有数值 j，调用 customfunction.f(i, j) 后，返回的结果等于 z
     */
    public static List<List<Integer>> findSolution(CustomFunction customfunction,
                                                   int z) {
        List<List<Integer>> res = new ArrayList<>();
        int left = 1, right = 1000;
        while (left <= 1000 && right >= 1) {
            int ans = customfunction.f(left, right);
            if (ans == z) {
                res.add(Arrays.asList(left, right));
                left ++;
                right --;
            } else if (ans < z) {
                left++;
            } else {
                right--;
            }
        }
        return res;
    }
}

class CustomFunction {
//    public int f(int x, int y) {
//        return x + y;
//    }

    public int f(int x, int y) {
        return x * y;
    }
}
